FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM node20-dotnet9:1.0.0-alpha.2 AS build
ARG BUILD_CONFIGURATION=Release

WORKDIR /src
COPY ["nuget.config", "./"]
COPY ["solid-quick-certs/solid-quick-certs.esproj", "solid-quick-certs/"]
COPY ["solid-quick-certs/package.json", "solid-quick-certs/"]
COPY ["solid-quick-certs/pnpm-lock.yaml", "solid-quick-certs/"]
COPY ["solid-quick-certs/.npmrc", "solid-quick-certs/"]
COPY ["Quickly.Certs.Core/Quickly.Certs.Core.csproj", "Quickly.Certs.Core/"]
COPY ["Quickly.Certs.EntityFrameworkCore/Quickly.Certs.EntityFrameworkCore.csproj", "Quickly.Certs.EntityFrameworkCore/"]
COPY ["Quickly.Certs.PostgresMigrations/Quickly.Certs.PostgresMigrations.csproj", "Quickly.Certs.PostgresMigrations/"]
COPY ["Quickly.Certs.SqliteMigrations/Quickly.Certs.SqliteMigrations.csproj", "Quickly.Certs.SqliteMigrations/"]
COPY ["Quickly.Certs.WebApi/Quickly.Certs.WebApi.csproj", "Quickly.Certs.WebApi/"]

RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages dotnet restore "./Quickly.Certs.WebApi/Quickly.Certs.WebApi.csproj"

WORKDIR "/src/solid-quick-certs"
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV COREPACK_NPM_REGISTRY="https://registry.npmmirror.com"
RUN corepack enable
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install

WORKDIR "/src"
COPY . . 
RUN dotnet build "./Quickly.Certs.WebApi/Quickly.Certs.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Quickly.Certs.WebApi/Quickly.Certs.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false 

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Quickly.Certs.WebApi.dll"]
